﻿@model IEnumerable<BussinessLogic.Models.Administrator.SessionExercise>
@{
    ViewBag.Title = "UserSessionDetails";
    var freeAnswer = false;
}
@foreach (var exercise in Model)
{
    <fieldset>
        <legend>@exercise.Name</legend>
        @if (exercise.ExType == BussinessLogic.Models.Administrator.ExerciseType.Exercise)
        {
            <br />
            <table>
                <tr>
                    <th>
                        Type
                    </th>
                    <th>
                        Convergent
                    </th>
                    <th>
                        Letter
                    </th>
                    <th>
                        Letter Position
                    </th>
                    <th>
                        Neutral Position
                    </th>
                    <th>
                        Reaction
                    </th>
                    <th>
                        Correct
                    </th>
                    <th>
                        Neutral Picture
                    </th>
                    <th>
                        Negative Picture
                    </th>
                </tr>
                @foreach (var trial in exercise.ExerciseTrials)
                {

                    if (trial.TrialType == BussinessLogic.Exercise.ExerciseTrial.SequenceType.Fixation)
                    {
                    <tr>
                        <td>@trial.TrialType
                        </td>
                        <td>@trial.Digit
                        </td>
                        <td>
                        </td>
                        <td>
                        </td>
                        <td>
                        </td>
                        <td>
                        </td>
                        <td>@(trial.Correct ? "Yes" : "No")
                        </td>
                        <td>
                            <td>
                            </td>
                        </td>
                    </tr>   
                    }
                    else
                    {
                    <tr>
                        <td>@trial.TrialType
                        </td>
                        <td>@(trial.Convergent ? "Convergent" : "Inconvergent")
                        </td>
                        <td>@trial.Letter
                        </td>
                        <td>@trial.LetterPosition
                        </td>
                        <td>@trial.NeutralPosition
                        </td>
                        <td>@trial.Reaction
                        </td>
                        <td>@(trial.Correct ? "Yes" : "No")
                        </td>
                        <td onclick="@("onPictureClick(true,'" + @trial.NeutralPicture + "')")">@trial.NeutralPicture
                        </td>
                        <td onclick="@("onPictureClick(false,'" + @trial.NegativePicture + "')")">@trial.NegativePicture
                        </td>
                    </tr>   
                    }
                }
            </table>
            <br />
        }
        else if (exercise.ExType == BussinessLogic.Models.Administrator.ExerciseType.Questionnaire)
        {
            freeAnswer = exercise.Questionnaire.Questions.Where(q => q.IsFreeAnswer).FirstOrDefault() != null;
            <p>
                Total score: @exercise.QNTotalScore</p>
            <table>
                <tr>
                    <th>
                        Text
                    </th>
                    @if (freeAnswer)
                    {
                        <th>
                            FreeAnswer
                        </th>
                    }
                    <th>
                        Score
                    </th>
                </tr>
                @foreach (var qn in exercise.Questionnaire.Questions)
                {
                    <tr>
                        <td>@qn.Text
                        </td>
                        @if (freeAnswer)
                        {
                            <td>@qn.FreeAnswer
                            </td>
                        }
                        <td>
                            @if (@qn.IsFreeAnswer)
                            {
                                <input type="text" id="@("freeAnswer_" + qn.FreeAnswerID) " name="FreeScore" value="@qn.Score"  style="width:24px" /> 
                            }
                            else
                            {
                                @qn.Score
                            }
                        </td>
                    </tr>
                }
            </table>
        }
        else if (exercise.ExType == BussinessLogic.Models.Administrator.ExerciseType.ExerciseMatrix)
        {
             <table>
                <tr>
                    <th>
                        Reaction
                    </th>
                    <th>
                        Neutral Picture
                    </th>
                </tr>
                @foreach (var trial in exercise.ExerciseMatrixTrials)
                {
                     <tr>
                        <td>@trial.Reaction
                        </td>
                        <td onclick="@("onPictureClick(true,'" + @trial.Picture + "')")">@trial.Picture
                        </td>
                    </tr>   
                }
                </table>
        }
        @if (exercise.Questionnaire != null && freeAnswer)
        {
            <br />
            <button type="button" value="Save Score" onclick="saveScore();">
                Save Score</button>
        }
    </fieldset>
}
@if (!string.IsNullOrEmpty(Request["sid"]))
{
    <input type="button" value="Approve to Study" onclick="@("approveUserToStudy(" + Request["sid"] + "," + Request["uid"] + ");")" />
}
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.19.min.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript">
    function approveUserToStudy(sId, uId) {
        $.post('@Url.Action("ApproveUserToSudy")', { 'studyId': sId, 'userId': uId }, function () {
            window.location = '@Url.Action("UserDetails", new { id = Request["uid"] })';
        });
    }

    function saveScore() {
        var result = new Array();
        var i = 0;
        $('[id^="freeAnswer_"]').each(function () {
            var id = $(this).attr('id').split('freeAnswer_')[1];
            var value = $(this).val();
            result[i] = new Object();
            result[i].ID = id;
            result[i].Value = value;
            i++;
        });

        $.post('@Url.Action("SaveScore")', { 'result': JSON.stringify(result) }, function () {
            //window.location = '@Url.Action("UserDetails", new { id = Request["uid"] })';
            location.reload(true);

        });

    }




    function onPictureClick(neutral, fname) {

        var $pictureDialog = $('<div></div>').dialog({
            autoOpen: false,
            resizable: false,
            title: fname,
            height: 330,
            width: 240,
            buttons: {
            }
        });

        if (neutral) {
            $pictureDialog.html('<img src="/uploads/neutral/' + fname + '" alt="" />');
        } else {
            $pictureDialog.html('<img src="/uploads/negative/' + fname + '" alt="" />');
        }
        $pictureDialog.dialog('open');
    }

</script>
